/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.notice.vo;

import io.swagger.annotations.ApiModelProperty;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.notice.entity.BizMsgConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import io.swagger.annotations.ApiModel;
import org.springblade.modules.notice.enums.ChannelType;
import org.springblade.modules.notice.enums.PushObjectEnum;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务消息管理表视图实体类
 *
 * @author ysq
 * @since 2024-11-20
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "BizMsgConfigVO对象", description = "业务消息管理表")
public class BizMsgConfigVO extends BizMsgConfig {
	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "1-短信，2-公众号，3-电话，4-飞书-机器人，5-app推送，6-梯无忧pc")
	private Integer channelType;

	private String channelTypes;

	private String channelTypeDesc;

	/**
	 * 推送对象展示数据
	 */
	private List<Map<String, String>> pushTypeDescList;

	private String pushTypeDesc;

	private String tenantId;

	public String getChannelTypeDesc() {
		if (Func.isNotBlank(channelTypes)) {
			List<Integer> channelTypeList = Func.toIntList(channelTypes);
			Map<Integer, Long> channelTypeMap = channelTypeList.stream().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
			return channelTypeMap.entrySet().stream().map(e -> String.format("%s (%d) ", ChannelType.getDesc(e.getKey()), e.getValue())).collect(Collectors.joining("/"));
		}
		return channelTypeDesc;
	}

	public List<Map<String, String>> getPushTypeDescList() {
		if (Func.isNotBlank(this.pushTypeDesc)) {
			List<Map<String, String>> result = new ArrayList<>();
			Map<String, List<String>> pushTypeDescMap = Func.toStrList("%", this.pushTypeDesc).stream().collect(Collectors.groupingBy(s -> s.split("_")[0]));
			pushTypeDescMap.forEach((k, v) -> {
				Map<String, String> itemDescMap = new HashMap<>(2);
				itemDescMap.put("channelName", ChannelType.getDesc(Integer.parseInt(k)));
				String valueSplits = v.stream().map(s -> s.split("_")[1]).collect(Collectors.joining(","));
				String pushTypes = Arrays.stream(valueSplits.split(",")).distinct().map(t ->  PushObjectEnum.getDesc(Integer.parseInt(t))).collect(Collectors.joining("/"));
				itemDescMap.put("pushName", pushTypes);
				result.add(itemDescMap);
			});
			return result;
		}
		return pushTypeDescList;
	}
}
